##########################################################################
# HANGARTNER, LAUDERDALE, SPIRIG (2025) INFERRING INDIVIDUAL PREFERENCES #
##########################################################################

####################################
########### DESCRIPTIVES ###########
####################################

rm(list=ls())



#############
# Libraries #
#############


library(dplyr)


options(scipen=999)


########
# DATA #
########

# decisions data (not in replication package)
aadecisionsloc <- "Data/AADecisions.csv"
decisions <- read.csv(aadecisionsloc)

# lawyer/paralegal variable
aadataloc <- "Data/aadata.Rda"
load(aadataloc)

Data <- left_join(decisions, DataL)
rm(DataL, decisions)

# judges data
jdataloc <- "Data/jdata.Rda"
load(jdataloc)


################
# DESCRIPTIVES #
################

# n cases
n_cases <- nrow(Data)
n_cases

# n unique judges
length(na.omit(unique(c(Data$J1, Data$J2, Data$J3))))

# when decisions were made
min(Data$year_decision)
max(Data$year_decision)

# number unified
table(Data$Vereinigt)

# number written off
table(Data$Outcome)[1] # written off

# cases receiving "other" decision 
print(paste0("cases that received `another' decision: ", table(Data$Outcome)[5])) # other

# remove these cases:
Data <- Data[!Data$Outcome %in% c("Abschreibung", "other"),]

# cases decided by 5 judges
table(Data$panel5==1)
# cases decided by a single judge
table(Data$single==1)
# cases decided under the simplified procedure
table(Data$twonly) 

# estimation sample focuses on cases decided by three judges
Data <- Data[Data$panel3==1,]

# Take only cases decided by/assigned to judges that were at the court before end of defined time period
keep <- Data$J1 %in% JData$TAG & Data$J2 %in% JData$TAG & Data$J3 %in% JData$TAG
print(paste0("Cases handled by judges that were not yet at the court in 2007: ", sum(keep==FALSE)))
Data <- Data[keep,]
    
# number of cases in estimation sample
nrow(Data)

# share of cases in estimation sample
print(paste0("% of cases in estimation sample: ", round((nrow(Data)/n_cases)*100, 1)))

# number of cases for which we know about lawyer/paralegal
table(!is.na(Data$lawyer))
print(paste0("% of published cases in estimation sample: ", round((sum(!is.na(Data$lawyer))/nrow(Data))*100,0)))

print(paste0("Average grant rate estimation sample: ", round(mean(Data$granted)*100,1), "%"))

# Number of cases by chair judge party
Data$Party <- gsub("_\\d{1,2}$", "", Data$J1)
print("Cases by chair judge party:")
print(table(Data$Party))




